<?php
/**
 * TbSyncLogDao.php class file.
 * @author shengxia <523247758@qq.com>
 * @date 2021/01/06- 5:13 下午
 */

namespace common\dao;


use common\exception\ValidatorException;
use common\models\PddBatchSyncLog;
use common\models\PddBatchSyncLogProducts;

class PddSyncBatchLogDao
{
    /**
     * 写入
     * @param array $data
     * @return PddSyncLog
     * @throws ValidatorException
     */
    public function insertData(array $data)
    {
        $syncLog = new PddBatchSyncLog();

        $syncLog->load($data,'');

        if (!$syncLog->validate()) {
            throw new ValidatorException($syncLog->errorsToString());
        }

        if($syncLog->save())
        {
            return $syncLog;
        }else{
            throw new ValidatorException($syncLog->errorsToOneString());
        }
    }

    /**
     * 写入
     * @param array $data
     * @return PddSyncLog
     * @throws ValidatorException
     */
    public function insertDataProducts(array $data)
    {
        $syncLog = new PddBatchSyncLogProducts();

        $syncLog->load($data,'');

        if (!$syncLog->validate()) {
            throw new ValidatorException($syncLog->errorsToString());
        }

        if($syncLog->save())
        {
            return $syncLog;
        }else{
            throw new ValidatorException($syncLog->errorsToOneString());
        }
    }

    /**
     * 获取最新一条同步记录
     * @param int $id
     * @return array|\yii\db\ActiveRecord|null
     */
    public function getLastLogInfo(int $id)
    {
        return PddBatchSyncLog::find()
            ->where(['id'=>$id])
            ->one();
    }

    /**
     * 通过主键获取信息
     * @param int $id
     * @return PddBatchSyncLog|null
     */
    public function getInfoByPk(int $id)
    {
        return PddBatchSyncLog::findOne(['id'=>$id]);
    }

    /**
     * 通过主键获取信息
     * @param int $id
     * @return PddBatchSyncLog|null
     */
    public function getInfoByPkProducts(int $batch_log_id,int $uid,int $mall_id,int $goods_id)
    {
        return PddBatchSyncLogProducts::findOne(['batch_no'=>$batch_log_id,'uid'=>$uid,'mall_id'=>$mall_id,'goods_id'=>$goods_id]);
    }

    /**
     * 通过主键获取信息
     * @param int $id
     * @return PddBatchSyncLog|null
     */
    public function getInfoByPkProductsById(int $batch_log_id)
    {
        return PddBatchSyncLogProducts::findOne(['id'=>$batch_log_id]);
    }

}
